package com.hh.form.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hh.baseframework.util.check.Check;
import com.hh.hibernate.dao.inf.IHibernateDAO;

@Service
public class CreateTableService {
	@SuppressWarnings("rawtypes")
	@Autowired
	private IHibernateDAO hibernateDAO;

	public void createTable(String tableName, List<String> sqlList,
			boolean deleteTable, int count) {

		if (count > 0) {
			if (deleteTable) {
				hibernateDAO.executeSql("drop table " + tableName);
			}
		}
		for (String string : sqlList) {
			hibernateDAO.executeSql(string);
		}

	}

	public int isDataBaseTable(String tableName) {
		int count = hibernateDAO
				.findCountBySql(
						"select COUNT(*) from TABS t where lower(t.table_name)=lower(?)",
						new Object[] { tableName });
		return count;
	}

	public void deleteTableByTableName(String tableName) {
		hibernateDAO.executeSql("drop table " + tableName);
	}

	public List<Map<String, Object>> queryColumnsByTableName(String tableName) {
		if (Check.isEmpty(tableName)) {
			return new ArrayList<Map<String, Object>>();
		}
		return hibernateDAO.queryListBySql(
				"Select * From user_tab_columns t where table_name ='"
						+ tableName.toUpperCase() + "'",
				new HashMap<String, Object>());
	}

	public void deleteColumnByIdsTabName(List<String> strToList,
			String tableName) {
		for (String string : strToList) {
			hibernateDAO.executeSql("alter table " + tableName
					+ " drop column " + string + "");
		}
	}
}
